package net.lahuobao.finance.entity.account;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.jeecgframework.core.util.StringUtil;

import net.lahuobao.finance.entity.organizationEntity.OrganizationEntity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Administrator on 2016/8/28.
 *
 * 账户信息实体
 *
 */
@Entity
@Table(name = "lahuobao.account", schema = "")
@DynamicUpdate(true)
@DynamicInsert(true)
public class AccountEntity {


    private long accountId;                                     //序号
    private long organizationId;                                //用户编号
    private String bankCardNo;                                  //银行卡号
    private String bankAccountName;                             //银行卡用户名称
    private String bankType;                                    //开户银行
    private String bankAddress;                                 //开户银行地址
    private String alipayNo;                                    //支付宝账号
    private String alipayName;                                  //支付宝账户名称
    private BigDecimal total;                                       //账户余额
    private BigDecimal frozen;                                      //账户冻结金额
    private BigDecimal usable;                                      //可用金额
    private Date   operateTime;                                 //操作时间
    private long   operateId;                                   //操作人编号
    private int    accountType;                                 //账户类型（0：普通账户 1：组织机构  2：组织机构成员）
    private String aboutId;                                     //组织机构ID

    
	@Id
    @Column(name = "account_id",length = 11)
    public long getAccountId() {
        return accountId;
    }

    public void setAccountId(long accountId) {
        this.accountId = accountId;
    }

    @Column(name = "account_Type",length = 6)
    public int getAccountType() {
        return accountType;
    }

    public void setAccountType(int accountType) {
        this.accountType = accountType;
    }

    @Column(name = "alipay_name",length = 50)
    public String getAlipayName() {
        return alipayName;
    }

    public void setAlipayName(String alipayName) {
        this.alipayName = alipayName;
    }

    @Column(name="alipay_no",length = 50)
    public String getAlipayNo() {
        return alipayNo;
    }

    public void setAlipayNo(String alipayNo) {
        this.alipayNo = alipayNo;
    }

    @Column(name = "bank_account_name",length = 50)
    public String getBankAccountName() {
        return bankAccountName;
    }

    public void setBankAccountName(String bankAccountName) {
        this.bankAccountName = bankAccountName;
    }

    @Column(name = "bank_address",length = 60)
    public String getBankAddress() {
        return bankAddress;
    }

    public void setBankAddress(String bankAddress) {
        this.bankAddress = bankAddress;
    }

    @Column(name = "bank_card_no",length = 30)
    public String getBankCardNo() {
        return bankCardNo;
    }

    public void setBankCardNo(String bankCardNo) {
        this.bankCardNo = bankCardNo;
    }

    @Column(name = "bank_type",length = 40)
    public String getBankType() {
        return bankType;
    }

    public void setBankType(String bankType) {
        this.bankType = bankType;
    }


	@Column(name = "frozen")
    public BigDecimal getFrozen() {
        return frozen;
    }

    public void setFrozen(BigDecimal frozen) {
        this.frozen = frozen;
    }

    @Column(name = "operate_Id",length = 11)
    public long getOperateId() {
        return operateId;
    }

    public void setOperateId(long operateId) {
        this.operateId = operateId;
    }

    @Column(name = "operate_Time")
    public Date getOperateTime() {
        return operateTime;
    }

    public void setOperateTime(Date operateTime) {
        this.operateTime = operateTime;
    }

    @Column(name = "organization_id")
    public long getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(long organizationId) {
        this.organizationId = organizationId;
    }

    @Column(name = "total")
    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    @Column(name = "usable")
    public BigDecimal getUsable() {
        return usable;
    }

    public void setUsable(BigDecimal usable) {
        this.usable = usable;
    }

    @Column(name = "about_id",length = 30)
    public String getAboutId() {
        return aboutId;
    }

    public void setAboutId(String aboutId) {
        this.aboutId = aboutId;
    }
    /**
     * 解冻并且将资金划归到可用余额,传入正整数!
     * 提现申请驳回
     *
     * @param amount
     * @return false 冻结金额不够解冻
     */
    public boolean unFrozen(BigDecimal amount) {
        /*
        amount = Math.abs(amount); //正整数计算
		if (frozen < amount) {
			return false;
		}
		frozen = frozen - amount;
		usable = usable + amount;
		return true;
        * */


        amount = amount.abs();
        if (amount.compareTo(frozen) == 1) {
            return false;
        }

        frozen = frozen.subtract(amount);
        usable = usable.add(amount);
        return true;
    }

    /**
     * 解冻并且将资金划到对方帐号
     * 用于提现完成
     *
     * @param amount
     * @return false 账户错误，冻结资金不足！
     */
    public boolean unFrozen4Withdraw(BigDecimal amount) {
        /*
        amount = Math.abs(amount); //正整数计算
		if (frozen < amount) {
			return false;
		}
		frozen = frozen - amount;
		total = total - amount;
		return true;
        * */

        amount = amount.abs(); //正整数计算
        if (amount.compareTo(frozen) == 1) {
            return false;
        }
        frozen = frozen.subtract(amount);
        total = total.subtract(amount);
        return true;
    }


    /**
     * 冻结可用金额,传入正整数!
     * 用于提现
     *
     * @return false 可用金额不够冻结
     */
    public boolean doFrozen(BigDecimal amount) {
        /*
        amount = Math.abs(amount); //正整数计算
        usable = usable - amount;
        frozen += amount;
        if (usable < 0) {
            return false;
        }
        return true;*/

        amount = amount.abs(); //正整数计算
        usable = usable.subtract(amount);
        frozen = frozen.add(amount);
        if (usable.compareTo(BigDecimal.ZERO) == -1) {
            return false;
        }
        return true;
    }


    /**
     * 充值，添加可用、总计金额
     */
    public void addUsable(BigDecimal amount) {

        /*
        this.usable += amount;
        this.total += amount;
        */
        usable = usable.add(amount);
        total = total.add(amount);
    }


    /**
     * 充值，添加可用、总计金额
     */
    public void subtractUsable(BigDecimal amount) {

        /*
        this.usable += amount;
        this.total += amount;
        */
        usable = usable.subtract(amount);
        total = total.subtract(amount);
    }

    // 获取账户识别码
    //private final static int identificationInt = 100000000;
    public String obtainIdentificationCode() {
    	int identificationInt = 100000000;
        String identification = "ORUC1X";
        int code = identificationInt + (int)this.accountId;
        String identificationCode = identification + code;
        return identificationCode;
    }

    public static int obtainAccountIdFromIdentificationCode(String identificationCode) {
    	int identificationInt = 100000000;

    	if (identificationCode.length() != 15) {
            return -1;
        }
        String sCode = identificationCode.substring(6);
        try {
            int code = Integer.valueOf(sCode);
            int accountId = code - identificationInt;
            return accountId;
        }
        catch(Exception ex)
        {
            return -1;
        }
    }
}
